package com.yunji.web.product;

import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.product.bo.AttrListQueryBO;
import com.yunji.product.business.ProductAttrBusiness;
import com.yunji.product.dto.AttrDTO;
import com.yunji.product.vo.AttrVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.web.product
 * @Title: ProductAttrController
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-14 10:54:38      V1.0        HuaAo       新建类
 */
@Api(tags = "管理端-商品属性")
@RestController
@RequestMapping("/container/attr")
public class ProductAttrController extends BaseController {

    @Resource
    ProductAttrBusiness business;

    /**
     * 查询商品属性列表
     */
    @ApiOperation("查询商品属性列表")
    @GetMapping("/list")
    public Response<List<AttrVo>> list(@Valid AttrListQueryBO param) {
        List<AttrVo> list = business.search(param);
        return ResponseHelper.successWithPage(list);
    }

    /**
     * 导出商品属性列表
     */
    @ApiOperation("导出商品属性列表")
    @Log(title = "商品属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AttrListQueryBO param) {
        List<AttrVo> list = business.search(param);
        ExcelUtil<AttrVo> util = new ExcelUtil<AttrVo>(AttrVo.class);
        util.exportExcel(response, list, "商品属性数据");
    }

    /**
     * 获取商品属性详细信息
     */
    @ApiOperation("获取商品属性详细信息")
    @GetMapping(value = "/{id}")
    public Response<AttrVo> getInfo(@PathVariable("id") Long id) {
        return ResponseHelper.success(business.select(id));
    }

    /**
     * 新增商品属性
     */
    @ApiOperation("新增商品属性")
    @Log(title = "商品属性", businessType = BusinessType.INSERT)
    @PostMapping
    public Response<Long> create(@RequestBody AttrDTO dto) {
        return ResponseHelper.success(business.create(dto));
    }

    /**
     * 修改商品属性
     */
    @ApiOperation("修改商品属性")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response<String> update(@RequestBody AttrDTO dto) {
        business.update(dto);
        return ResponseHelper.success("修改成功");
    }

    /**
     * 删除商品属性
     */
    @ApiOperation("删除商品属性")
    @Log(title = "商品属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Response<String> remove(@PathVariable Long... ids) {
        business.delete(ids);
        return ResponseHelper.success("删除成功");
    }

    @ApiOperation("查看商品属性是否被应用")
    @GetMapping("/inUse/{attrId}")
    public Response<Boolean> inUse(@PathVariable Long attrId) {
        return ResponseHelper.success(business.inUse(attrId));
    }

}